﻿using System;
using System.Collections.Generic;
using System.Text;
using He.Common;
using LitJson;
using System.Web;

namespace He.API.OAuth.QQHelper
{
    public class QQHelper
    {
        public string oauth_app_id = "";
        public string oauth_app_key = "";
        public Model.webset webset;
        public Model.site_info siteinfo;
        public QQHelper()
        {
            webset = new BLL.webset().GetWebset();
            this.oauth_app_id = webset.qqappid;
            this.oauth_app_key = webset.qqappkey;
        }

        public void send_state(string rtnPageName)
        {
            string return_uri = string.Format("http://{0}{1}user/oauth/qq/{2}", webset.site_domain.Trim('/'), webset.web_path, rtnPageName);
            Random random = new Random();
            HttpContext.Current.Session["state"] = CryptoHelper.MD5(random.Next().ToString());
            string send_url = string.Concat(new object[] { "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=", oauth_app_id, "&redirect_uri=", HttpContext.Current.Server.UrlEncode(return_uri), "&state=", HttpContext.Current.Session["state"] });
            HttpContext.Current.Response.Redirect(send_url);
        }

        public Dictionary<string, object> get_access_token(string code, string rtnPageName)
        {
            string return_uri = string.Format("http://{0}/user/oauth/qq/{1}", webset.site_domain.Trim('/'), rtnPageName);
            string result = HttpHelper.HttpGet("https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=" + this.oauth_app_id + "&redirect_uri=" + HttpContext.Current.Server.UrlEncode(return_uri) + "&client_secret=" + this.oauth_app_key + "&code=" + code);
            if (result.Contains("error"))
            {
                return null;
            }
            try
            {
                string[] parm = result.Split(new char[] { '&' });
                string access_token = parm[0].Split(new char[] { '=' })[1];
                string expires_in = parm[1].Split(new char[] { '=' })[1];
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("access_token", access_token);
                dic.Add("expires_in", expires_in);
                return dic;
            }
            catch
            {
                return null;
            }
        }

        public JsonData get_info(string access_token, string open_id)
        {
            string result = HttpHelper.HttpGet("https://graph.qq.com/user/get_info?access_token=" + access_token + "&oauth_consumer_key=" + this.oauth_app_id + "&openid=" + open_id);
            if (!result.Contains("error"))
            {
                try
                {
                    JsonData jd = JsonMapper.ToObject(result);
                    if (jd.Count > 0)
                    {
                        return jd;
                    }
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }

        public Dictionary<string, object> get_open_id(string access_token)
        {
            string result = HttpHelper.HttpGet("https://graph.qq.com/oauth2.0/me?access_token=" + access_token);
            if (result.Contains("error"))
            {
                return null;
            }
            int str_start = result.IndexOf('(') + 1;
            int str_last = result.LastIndexOf(')') - 1;
            return JsonMapper.ToObject<Dictionary<string, object>>(result.Substring(str_start, str_last - str_start));
        }

        public Dictionary<string, object> get_user_info(string access_token, string open_id)
        {
            string result = HttpHelper.HttpGet("https://graph.qq.com/user/get_user_info?access_token=" + access_token + "&oauth_consumer_key=" + this.oauth_app_id + "&openid=" + open_id);
            if (result.Contains("error"))
            {
                return null;
            }
            return JsonMapper.ToObject<Dictionary<string, object>>(result);
        }
    }
}
